//
//  XYPopView.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/11.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

protocol XYPopViewDelegate:NSObjectProtocol{
    
    func PopViewWillDismiss()
}

class XYPopView: UIButton {
    
    weak var delegate:XYPopViewDelegate?
    
    var imgView:UIImageView?

    init(contenView:UIView){
        //创建遮罩view
        super.init(frame:UIScreen.mainScreen().bounds)
      
        self.addTarget(self, action: "didColseView:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //创建灰色框
        let imgView = UIImageView(image: UIImage(named: "popover_background")?.resizableImageWithCapInsets(UIEdgeInsetsMake(20, 0, 20, 0), resizingMode: UIImageResizingMode.Stretch))
        imgView.userInteractionEnabled = true
        imgView.width = contenView.width + 10
        imgView.height = contenView.height + 24
        imgView.centerX=self.centerX
        self.imgView = imgView
        self.addSubview(imgView)
        
        //设置内容View的坐标
        contenView.x = 5
        contenView.y = 13
        imgView.addSubview(contenView)

    }
    //展示界面
    func show(titleButton:XYTitleButton){
        
        let rect = titleButton.convertRect(titleButton.bounds, toView: nil)
        self.imgView!.y = CGRectGetMaxY(rect)
        //获得当前的窗口
        let window = UIApplication.sharedApplication().windows.last
        window?.addSubview(self)
    }
    
    //点击屏幕移除view
    @objc private func didColseView(backeView:UIButton){
        
        backeView.removeFromSuperview()
        if let d = delegate{
            d.PopViewWillDismiss()
        }
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
